package com.instalinks.android.apps;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;

import com.instalinks.android.apps.R;
import com.instalinks.android.apps.helper.AppConfig;
import com.instalinks.android.apps.service.AppService;
import com.instalinks.android.lib.AppHelper;

public class HomeActivity extends Activity {
    
	private Button btnRegisterNotification;
	private ToggleButton btnServiceOnOff;
	private AppConfig mAppCfg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.initialize();
        
    }
    
    private void initialize(){
    	
    	mAppCfg = new AppConfig(getApplicationContext());
    	
    	btnRegisterNotification = (Button)findViewById(R.id.hm_btnRegisterNotification);
    	btnServiceOnOff = (ToggleButton)findViewById(R.id.hm_btnServiceOnOff);
    	
    	setToggleMode(mAppCfg.isServiceRunning());
		
		
		btnServiceOnOff.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (btnServiceOnOff.isChecked()) {
					startServiceThread();
				} else {
					stopServiceThread();
				}
			}
		});
		
		btnRegisterNotification.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				new C2DMRegistrationTask().execute();
			}
		});
		
		
		mAppCfg.notifyServiceState();
		
		
		if(mAppCfg.getDataPostUrl().length()==0){
			try {
				String url = mAppCfg.getAppUrl();
				mAppCfg.setDataPostUrl(url);
			} catch (Exception e) {
				AppHelper.alert(getApplicationContext(), "Error loading DataPostUrl : " + e.getMessage());
			}
		}
		
		if(!mAppCfg.isServiceRunning()){
			this.startServiceThread();
		}
		
		if(mAppCfg.getRegistrationId().length()<10){
			new C2DMRegistrationTask().execute();
		}
		
		setToggleMode(mAppCfg.isServiceRunning());
    }
    
    private void startServiceThread(){
    	startService(new Intent(this, AppService.class));
    	
    	setToggleMode(true);
    }
    
    private void stopServiceThread(){
    	stopService(new Intent(this, AppService.class));
    	
    	setToggleMode(false);
    }
    
    private void setToggleMode(boolean isChecked){
    	
    	this.btnServiceOnOff.setChecked(isChecked);
//    	this.btnServiceOnOff.setEnabled(!isChecked);
    	
    	btnServiceOnOff.setBackgroundResource(isChecked?R.drawable.toggle_on:R.drawable.toggle_off);
    	
    }
    
    private class C2DMRegistrationTask extends AsyncTask<Void, Void, Void> {

		ProgressDialog pd;

		@Override
		protected void onPreExecute() {
			String label = "Registering For Push Notifications";
			pd = ProgressDialog.show(HomeActivity.this, "", label);
		}

		@Override
		protected Void doInBackground(Void... voids) {
			mAppCfg.registerForC2dm(true);
			return null;
		}

		@Override
		protected void onPostExecute(Void voids) {
			pd.dismiss();
			pd.cancel();
		}
	}

}